<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class RegController extends CommonController {
	//空操作
	public function _initialize(){
		parent::_initialize();
	}
    /**
     * 填写机构码注册
     */
  	public function checkCode(){
  		$code=I('code');
  		$r=M('Admin')->where('admin_id ='.$code)->find();
  		if($r){
  			$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".S('assess_token')."&openid=".$_SESSION['openid']."&lang=zh_CN";
  			//dump($get_user_info_url);
  			$user_info =$this->curl_get($get_user_info_url); 			
  			$email= $user_info['nickname'];
  			$head = $user_info['headimgurl'];
  			//dump($email);die;
  			//$email= $user_info->nickname;
  			//$head = $user_info->headimgurl;
  			$arr['openid']=$_SESSION['openid'];
  			$arr['email']=$email;
  			$arr['nick']=$email;
  			$arr['head']=$head;
  			$arr['status']=0;
  			$arr['pwd']=md5('111111');
  			$arr['reg_time']=time();
  			$arr['is_userInvite']=2;
  			$arr['pid']=$r['id'];
  			$a = M('Member')->add($arr);
  			if($a){
  				session('member_id',$a);//传入session避免直接进入个人信息界面
  				session('USER_KEY',$email);//用户名
  				session('STAUTS',0);
  				session('procedure',1);
  				$data['status']=1;
  				$data['info']="登录成功";
  				$this->ajaxReturn($data,'JSONP');
  			}else{
  				$data['status']=0;
  				$data['info']="服务器繁忙，请稍后再试";
  				$this->ajaxReturn($data,'JSONP');
  			}			
  		}else{
  			$data['status']=0;
  			$data['info']="机构码错误";
  			$this->ajaxReturn($data,'JSONP');
  		}
  	}
  	public function checkCode2(){
  		$code=I('code22');
  		$pid=M('Member')->where('openid ='.$code)->find()['member_id'];  				
  			$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".S('assess_token')."&openid=".$_SESSION['openid']."&lang=zh_CN";
  			//dump($get_user_info_url);
  			$user_info =$this->curl_get($get_user_info_url);
  			$email= $user_info['nickname'];
  			$head = $user_info['headimgurl'];
  			//dump($email);die;
  			//$email= $user_info->nickname;
  			//$head = $user_info->headimgurl;
  			$arr['openid']=$_SESSION['openid'];
  			$arr['email']=$email;
  			$arr['nick']=$email;
  			$arr['head']=$head;
  			$arr['status']=0;
  			$arr['pwd']=md5('111111');
  			$arr['reg_time']=time();
  			$arr['pid']=$pid;
  			$arr['is_userInvite']=1;
  			$a = M('Member')->add($arr); 			
  			session('member_id',$a);//传入session避免直接进入个人信息界面
  			session('USER_KEY',$email);//用户名
  			session('STAUTS',0);
  			session('procedure',1); 				
  			Header("Location: http://pan2.money654.com/?code=".$_GET["code"]."&state=#!//indexPerson/");
  	}
  	 
    //使用curl的GET请求数据  返回数组
    protected function curl_get($http){
    	//初始化
    	$ch = curl_init();
    	//设置选项，包括URL
    	curl_setopt($ch, CURLOPT_URL, $http);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	//执行并获取HTML文档内容
    	$output = curl_exec($ch);
    	//释放curl句柄
    	curl_close($ch);    	
    	//打印获得的数据
    	$result = json_decode($output,true);
    	return $result;
    }
    
    public function ercode(){
    	$url="http://pan2.money654.com/Home/Reg/checkCode?code=12";
    	$code="http://www.gbtags.com/gb/qrcode?t=".$url."&s=400";    	
    }
}